﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace Service
{
    public class PubService
    {
        readonly ISubscriber subscriber;
        readonly ConnectionMultiplexer redisConnection;
        string tcp = "127.0.0.1:6379";
        string channel = "";

        /// <summary>
        /// 发布服务
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="tcpStr"></param>
        public PubService(string channelName, string tcpStr = null)
        {
            tcp = tcpStr ?? tcp;
            redisConnection = ConnectionMultiplexer.Connect(tcp);
            subscriber = redisConnection.GetSubscriber();
            channel = channelName;
        }

        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="tcpStr"></param>
        public void Publish(string message, string channelStr = null)
        {
            channelStr = channelStr ?? channel;
            subscriber.Publish(channelStr, message);
        }
    }
}
